/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package procrastinator.table;

import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Formatter;
import javax.swing.table.DefaultTableCellRenderer;
import procrastinator.table.ActivityTable.Period;

/**
 *
 * @author alex
 */
public class PeriodRenderer extends DefaultTableCellRenderer {
    private DateFormat format1, format2;

    public PeriodRenderer() {
        super();

        format1 = new SimpleDateFormat("dd/MM/yy, HH:mm");
        format2 = new SimpleDateFormat("HH:mm");
    }

    @Override
    public void setValue(Object value) {
        if ((value != null) && (value instanceof Period)) {
            StringBuffer buf = new StringBuffer();
            Period p = (Period) value;
            
            buf.append(format1.format(p.from));
            buf.append(" - ");
            buf.append(format2.format(p.to));
            
            new Formatter(buf).format(" (%d:%02dh)", p.duration()/60, p.duration()%60);
            
            value = buf.toString();
        }

        super.setValue(value);
    }
}
